/**
 * 大厅三级按钮组件
 */
var HallLv3Unit = Unit.extend({
    //更多按钮的最大数
    _numMore: 4,
    //按钮列表: 0：click；1：name；
    _btnsList: [
        ['_clickSetting', 'btn_setting'],
        ['_clickShare', 'btn_share'],
        ['_clickHistory', 'btn_history'],
        ['_clickActivity', 'btn_activity'],
        ['_clickYaoqing', 'btn_yaoqing'],
        ['_clickWanFa', 'btn_rule'],
        ['_clickBD', 'btn_binding'],
        ['_clickCertified', 'btn_realname'],
        ['_clickMessage', 'btn_message'],
        ['_clickKefu', 'btn_kefu'],
        ['_clickExit', 'btn_ext1'],
        ['_clickShop', 'btn_shop'],

        ['_clickZhaoMu', 'btn_zhaomu'],
        /**
         * 通用功能
         */
        ['_clickSign', 'btn_sign'],
        ['_clickTicketShop', 'btn_ticket_shop'],
        ['_clickEmail', 'btn_email'],
        ['_clickBag', 'btn_bag'],
        ['_clickRedBag', 'btn_redbag'],

        //more
        ['_clickExt2', 'btn_ext2'],
        ['_clickExt3', 'btn_ext3'],
        ['_clickExt4', 'btn_ext4'],
    ],

    _ccsFileName: res.HallLv3Unit_json,
    initUnit:function () {
        this._inReview();
        this.btnMore = this._nodeList['btn_more'];
        this.bgMore = this._nodeList['bg_more'];
        safeVisible(this._nodeList['btn_binding'],isNullString(gameData.BDIphone));
        safeVisible(this._nodeList['btn_realname'],!gameData.hasShiMing);
        safeVisible(this._nodeList['btn_activity'],false);

        /**
         * 通用功能
         */
        safeVisible(this._nodeList['btn_sign'], hall_common_module.isOpenHallCommon);
        safeVisible(this._nodeList['btn_ticket_shop'], hall_common_module.isOpenHallCommon);
        safeVisible(this._nodeList['btn_email'], hall_common_module.isOpenHallCommon);
        safeVisible(this._nodeList['btn_bag'], hall_common_module.isOpenHallCommon);
        safeVisible(this._nodeList['btn_redbag'], hall_common_module.isOpenHallCommon);

    },
    /**
     * 初始化提审隐藏
     * @protected
     */
    _inReview: function () {
        if (!window.inReview) {
            return;
        }

    },
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'btn_more': '_clickMore',
            'bg_more': '_clickBg',
        };

        for (var i = 0; i < this._btnsList.length; i++) {
            var _name = this._btnsList[i][1];
            var btn = this._nodeList[_name];
            if (btn) {
                btn.setTag(i);
                this._clickListeners[_name] = '_clickButton';
            }
        }

    },
    //--TODO
    _initNetworkListeners: function () {
        var self = this;
        this._networkListeners = {
            // ReqRedPoint: '_onReqRedPoint',
            Certified: '_onCertified'
        };
        this._addEventListener(EventKey.Close, function () {
            self._nodeList['btn_binding'].setVisible(isNullString(gameData.BDIphone));
            //self._resetBtnPoint();
        });
        this._addEventListener(EventKey.Refresh_PlayerInfo, function () {
            self._nodeList['btn_realname'].setVisible(!gameData.hasShiMing);
            //self._resetBtnPoint();
        });
    },

    /***
     * 重置触摸按钮位置
     * @protected
     */
    _resetBtnPoint:function(){
        // var btnOrder = [
        //     'btn_setting',
        //     'unit_NoticeBtnUnit',
        //     'btn_rule',
        //     'btn_history',
        //     'btn_share',
        //     'btn_realname',
        //     'btn_binding',
        //     // 'btn_kefu'
        // ];
        //
        // var areaWidth = 120;
        // var startX = 248;
        // if( this._nodeList['btn_setting'] && cc.sys.isObjectValid(this._nodeList['btn_setting']) &&
        //     this._nodeList['btn_rule'] && cc.sys.isObjectValid(this._nodeList['btn_rule'])){
        //     areaWidth = this._nodeList['btn_rule'].x - this._nodeList['btn_setting'].x;
        //     startX = this._nodeList['btn_setting'].x;
        // }
        // var index = 0;
        // for (var i = 0; i < btnOrder.length; i++) {
        //     var nodeName = btnOrder[i];
        //     if(this._nodeList[nodeName] && cc.sys.isObjectValid(this._nodeList[nodeName]) && this._nodeList[nodeName].isVisible()){
        //         this._nodeList[nodeName].x = startX + index * areaWidth;
        //         index++;
        //     }
        // }

    },
    /**
     * 验证
     * @param data
     * @private
     */
    _onCertified: function (data) {
        var _data = data;
        if (_data.cmd == "VerificationCode") {  // 发送验证码成功
            if (_data.error_code == 0) {
                eventManager.dispatchEvent(EventKey.ScheYZM);
            } else {
                showAlert("您输入的手机号码有误，请重新输入")
            }
        } else if (_data.cmd == "BindMobile") {  //绑定成功
            if (_data.error_code == 0) {
                var str = "手机号绑定成功";
                if (_data.numof_cards_2 > 0) {
                    str = '手机号绑定成功，恭喜获得' + _data.numof_cards_2 + '张房卡';
                    network.send(3013);
                }
                gameData.BDIphone = _data.mobile;
                showAlert(str);
                eventManager.dispatchEvent(EventKey.Close);
                eventManager.dispatchEvent(EventKey.Refresh_PlayerInfo);

            } else {
                showAlert('您输入的验证码有误，请重新输入')
            }
        } else if (_data.cmd == "UnbindMobile") {
            if (_data.error_code == 0) {
                gameData.BDIphone = "";
                setTimeout(function () {
                    // cc.log("==解绑成功倒计时=");
                    showAlert('您输入的验证码有误，请重新输入')
                }, 200);
            } else {
                showAlert('解绑失败!')
            }
        } else {
            if (_data.success) {
                gameData.hasShiMing = true;
                showAlert('认证成功', function () {
                });
                eventManager.dispatchEvent(EventKey.Refresh_PlayerInfo);
            } else {
                showAlert('验证失败,请重试！')
            }
        }
    },

    /**
     * 请求红点--TODO
     * @param data
     * @private
     */
    _onReqRedPoint: function (data) {
        // cc.log("HallLv3Unit === _onReqRedPoint, data", data);
    },

    /**
     * 点击按钮时
     * @param btn:点击的按钮
     * @private
     */
    _clickButton: function (btn) {
        var tag = btn.getTag();

        var param = this._btnsList[tag];
        if (!param) return;
        //点击
        var _click = param[0];
        if (_click && _click.length > 0) {
            window.eventManager.dispatchEvent(EventKey.HallBoardBtnEvent, _click);
        }
    },
    /**
     * 点击更多
     * @private
     */
    _clickMore: function () {
        var _visible = this.bgMore.isVisible();

        this.bgMore.setVisible(!_visible);

        var _len = this._btnsList.length;
        for (var i = 1; i <= this._numMore; i++) {
            this._nodeList['btn_ext' + i].setVisible(!_visible);
        }
        this._nodeList['unit_NoticeBtnUnit'].setVisible(!_visible);
    },
    /**
     * 点击背景时(ps：以防点击到下面的活动按钮)
     * @private
     */
    _clickBg: function () {
        // cc.log("HallLv3Unit === _clickBg", this.bgMore.isVisible());
    },
});